/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sgsmcan.Dao;


import com.sgsmcan.Entidad.Usuario;
import com.sgsmcan.Hibernate.HibernateUtil;
import com.sgsmcan.Utils.Utilitario;
import java.io.Serializable;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author user
 */
public class UsuarioDaoImpl extends HibernateDao<Usuario> implements UsuarioDao <Usuario>, Serializable{
    
    final String CLASE_USUARIO = "[USUARIO-DAO-IMPL]";
    
    @Override
    public boolean validarNick(String nick){
        Session session = null;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            Query query = session.createQuery("from Usuario as user where user.nick like :dato");
            query.setParameter("dato", nick);
            Usuario aux = null;
            if(( aux=(Usuario) query.uniqueResult() ) == null || !aux.getNick().equals(nick)){
                return false;
            }
            
            return true;
        }catch(HibernateException e){
            Utilitario.LOG.error(CLASE_USUARIO+ " Error al validar el nick ", e);
            throw new HibernateException(e);
        }finally{
            if(session != null){
                session.close();
            }
        }
    }

    @Override
    public boolean validarClave(String nick, String clave) {
        Session session = null;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            Query query = session.createQuery("from Usuario as user where user.nick = :dato and user.clave = :data");
            query.setParameter("dato", nick);
            query.setParameter("data", clave);
            if(query.uniqueResult() == null){
                return false;
            }
            return true;
        }catch(HibernateException e){
            Utilitario.LOG.error(CLASE_USUARIO+ " Error al validar la clave ", e);
            throw new HibernateException(e);
        }finally{
            if(session != null){
                session.close();
            }
        }
    }

    @Override
    public Usuario getForID(String user) {
        Session session = null;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            Query query = session.createQuery("select user from Usuario as user join fetch user.rol as role join fetch role.permisos as permiso where user.nick = :dato order by permiso.modulo.id");
            query.setParameter("dato", user);
            return (Usuario)query.uniqueResult();
        }catch(HibernateException e){
            Utilitario.LOG.error(CLASE_USUARIO+ " Error al obtener Usuario ", e);
            throw new HibernateException(e);
        }finally{
            if(session != null){
                session.close();
            }
        }
    }

    @Override
    public List<Usuario> getUsuarios(String id) {
        Session session = null;
        List<Usuario> aux;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            Query query = session.createQuery("from Usuario as user where user.nick != :dato");
            query.setParameter("dato",id);
            aux = query.list();
            return aux;
        }catch(HibernateException e){
            Utilitario.LOG.error(CLASE_USUARIO+ " Error al obtener Usuario ", e);
            throw new HibernateException(e);
        }finally{
            if(session != null){
                session.close();
            }
        }
    }

    @Override
    public DetachedCriteria getUsers(String id) {
        DetachedCriteria criteria = DetachedCriteria.forClass(Usuario.class,"user").createAlias("user.rol","role").add(Restrictions.eq("user.estado",Boolean.TRUE)).add(Restrictions.ne("user.nick", id));
        return criteria;
    }

    @Override
    public boolean existeNick(String nick) {
        Session session = null;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            Query query = session.createQuery("from Usuario as user where user.nick like :dato");
            query.setParameter("dato", nick);
            Usuario aux = null;
            if(( aux=(Usuario) query.uniqueResult() ) == null){
                return false;
            }
            return true;
        }catch(HibernateException e){
            Utilitario.LOG.error(CLASE_USUARIO+ " Error al validar el nick ", e);
            throw new HibernateException(e);
        }finally{
            if(session != null){
                session.close();
            }
        }
    }
    
}
